package com.carlo.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 
* @ClassName: GlobalUtil 
* @Description: 工具类
* @author carlo
* @date 2018年2月24日 下午6:28:17
 */
public class GlobalUtil {
	
	
	/**
	 * 
	* @Title: clearHtmlLabel 
	* @Description: 去掉内容中含有的html标签 
	* @param @param content
	* @param @return    设定文件 
	* @return String    返回类型 
	* @author carlo
	* @throws
	 */
	public static String clearHtmlLabel(String content){
		LinkedHashMap<String, String> lableMap = new LinkedHashMap<String, String>(){
			private static final long serialVersionUID = 1L;
			{
				put("<html", "</html>");  
				put("<head", "</head>");  
				put("<body", "</body>");  
				put("<div", "</div>");  
				put("<table", "</table>");  
				put("<tbody", "</tbody>");  
				put("<td", "</td>");  
				put("<font", "</font>");  
				put("<iframe", "</iframe>");  
				put("<span", "</span>");  
				put("<strong", "</strong>");  
				put("<p", "</p>");
				put("<a", "</a>"); 
				put("<b>", "</b>"); 
				  
				put("<style", "</style>");  
				put("<!--", "-->");  
				put("<meta", ">"); 
				put("<img", ">"); 
				put("<hr", ">"); 

				put("\r\n", "");  //去掉html中的换行
				put("<br>", "\n");
				put("&nbsp;", "");

				
				put("<tr", "</tr>"); 
				 
			}
		};
		
		for(Map.Entry<String, String> entry : lableMap.entrySet()){
			content = clearHtmlLabel(content, entry.getKey(), entry.getValue());
		}
		return content;
	}
	/**
	 * 
	* @Title: clearHtmlLabel 
	* @Description: 去掉html的标签  ，如<style>xxxx</style> , leftLable = <style , rightLabel = </style> . 其他标签类似
	* @param @param leftLable 
	* @param @param rightLabel
	* @param @return    设定文件 
	* @return String    返回类型 
	* @author carlo
	* @throws
	 */
	public static String clearHtmlLabel(String content , String leftLable , String rightLabel){
		if(StringUtils.isEmpty(content)){
			return "";
		}
		//有一些标签是所有内容都不需要的，比如 style 等
		if(leftLable.equals("<!--") || leftLable.equals("<style") 
				|| leftLable.equals("<meta") || leftLable.equals("<head")
				|| leftLable.equals("<img") || leftLable.equals("<hr")){
			while(content.contains(leftLable)){
				int begin = content.indexOf(leftLable);
				String _left = content.substring(0 , begin);
				String _right = content.substring(begin);
				begin = _right.indexOf(rightLabel);
				content = _left + _right.substring(begin + rightLabel.length());
			}
		}else if(leftLable.equals("\r\n") || leftLable.equals("<br>") 
				|| leftLable.equals("&nbsp;")){ 
			content = content.replace(leftLable, rightLabel);
		}else{ //另外一些标签是需要获取标签所包含的内容
			//如 content的内容为 ： <body><div style="xxxxX">dddd<div>yyy</div></div></body>
			//先统一处理右侧标签
			if(rightLabel.equals("</td>")){		//td标签，则替换为空格 隔开
				content = content.replace(rightLabel, " ");
			}else if(rightLabel.equals("</tr>")){  //如果是</tr> 则替换为换行
				content = content.replace(rightLabel, "\n");
			}else{  //其他标签，则替换为""
				content = content.replace(rightLabel, "");
			}
			
			//此时 content的内容为 ：  <body><div style="xxxxX">dddd<div>yyy</body>
			//处理左侧标签，循环判断当前内容是否含有对应的标签，如果含有，则进行处理，清除掉它
			while(content.contains(leftLable)){
				int begin = content.indexOf(leftLable);
				String _left = content.substring(0 , begin);
				String _right = content.substring(begin + leftLable.length());
				begin = _right.indexOf(">");
				begin = begin < _right.length() - 1 ? begin + 1 : begin;
				content = _left + _right.substring(begin);
			}
		}
		return content;
	}
	
	
	/**
	 * 
	* @Title: writeInputStreamToFile 
	* @Description: 解析输入流存入到文件中
	* @param @param filePath
	* @param @param is
	* @param @return    设定文件 
	* @return String    返回类型 
	* @author carlo
	* @throws
	 */
	public static String writeInputStreamToFile(String destPath , InputStream is){
		BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
		try{
			File dest = new File(destPath);  
	        if(!dest.exists()) {  
	            dest.createNewFile();  
	        }  
	        bos = new BufferedOutputStream(new FileOutputStream(destPath));
            bis = new BufferedInputStream(is);
            int c;
            while ((c = bis.read()) != -1) {
                bos.write(c);
                bos.flush();
            }  
		}catch(Exception e){
			e.printStackTrace();
		} finally{
			try{
				if(null != is){
					is.close(); 
				}
				if(null != bis){
					bis.close(); 
				}
				if(null != bos){
					bos.close(); 
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return destPath;
	}

}
